package TestThread;

public class demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            System.out.println("通过异常来终止线程");
            int result = 1 / 0;
        });
        Thread thread2 = new Thread(() -> {
            System.out.println("通过interrupt来终止线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("收到中断信号，开始结束线程");
                return;
            }
        });

        thread1.start();
        thread1.join();
        System.out.println("线程1 "+thread1.getState());
        //thread1.start();抛出IllegalThreadStateException异常

        thread2.start();
        thread2.interrupt();
        thread2.join();
        System.out.println("线程2 "+thread2.getState());
    }
}
